public class demo{
    public static void main(String[] args) {
        //创建两个锁对象
        Object lock1 = new Object();
        Object lock2 = new Object();

        //创建子线程
        /*
         * 线程1：①先获得锁1 ②休眠1s，让线程2获得锁2 ③线程1尝试获取锁2 线程2同理
         */
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                //线程1业务逻辑
                synchronized (lock1) {
                    System.out.println("中国人得到了其中第1支筷子");
                    try {
                        //休眠1s，让线程2先得到锁2
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("中国人尝试获第2支筷子...");
                    synchronized (lock2) {
                        System.out.println("中国人成功获第2支筷子!");
                    }
                }
            }
        }, "中国人");

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                //线程2业务逻辑
                synchronized (lock2) {
                    System.out.println("美国人得到了刀");
                    try {
                        //休眠1s，让线程1先得到锁1；因为线程是并发执行我们不知道谁先执行
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("美国人尝试获取叉...");
                    synchronized (lock1) {
                        System.out.println("美国人成功获得了叉");
                    }
                }
            }
        }, "美国人");
        thread1.start();
        thread2.start();
    }
}


